import { queryDeptInfo, queryProvinceInfo, queryDeptCommonCode } from '@/api/info'
import { STATISTICS_WAY } from '@/const/const'

const deptInfo = {
  state: {
    // 省区信息
    provinceCommonList: [],
    // 处理部门信息
    productLineList: [],
    // 省区地市信息
    provinceTreeList: [],
    // deptCommonCode
    deptCommonCode: ''
  },
  getters: {
    provinceCommonList: (state) => state.provinceCommonList,
    productLineList: (state) => state.productLineList,
    provinceTreeList: (state) => state.provinceTreeList,
    deptCommonCode: (state) => state.deptCommonCode
  },
  mutations: {
    SET_PROVINCE_COMMON_LIST: (state, provinceCommonList) => {
      state.provinceCommonList = provinceCommonList
    },
    SET_PRODUCT_LINE_LIST: (state, productLineList) => {
      state.productLineList = productLineList
    },
    SET_PROVINCE_LIST: (state, provinceTreeList) => {
      state.provinceTreeList = provinceTreeList
    },
    SET_DEPT_COMMON_CODE: (state, deptCommonCode) => {
      state.deptCommonCode = deptCommonCode
    }
  },
  actions: {
    /** 获取省区信息 */
    getProvinceCommonList({ commit }) {
      const params = {
        statisticsWay: STATISTICS_WAY.province
      }
      return new Promise((resolve, reject) => {
        queryDeptInfo(params)
          .then((res) => {
            if (res.success) {
              commit('SET_PROVINCE_COMMON_LIST', res.result)
              resolve(res.result)
            } else {
              reject(res.message)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    /** 获取处理部门信息 */
    getProductLineList({ commit }) {
      const params = {
        statisticsWay: STATISTICS_WAY.productline
      }
      return new Promise((resolve, reject) => {
        queryDeptInfo(params)
          .then((res) => {
            if (res.success) {
              commit('SET_PRODUCT_LINE_LIST', res.result)
              resolve(res.result)
            } else {
              reject(res.message)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    /** 获取省区地市信息 */
    getProvinceTreeList({ commit }) {
      return new Promise((resolve, reject) => {
        queryProvinceInfo()
          .then((res) => {
            if (res.success) {
              const provinceList = res.result.dataList[0].children || []
              commit('SET_PROVINCE_LIST', provinceList)
              resolve(provinceList)
            } else {
              this.$tap.alert(res.message, 'warning')
              reject(res.message)
            }
          })
          .catch((err) => {
            console.error(err)
            reject(err)
          })
      })
    },
    /** 获取deptCommonCode信息 */
    getDeptCommonCodeInfo({ commit }) {
      return new Promise((resolve, reject) => {
        queryDeptCommonCode()
          .then((res) => {
            if (res.success) {
              commit('SET_DEPT_COMMON_CODE', res.result)
              resolve(res.result)
            } else {
              reject(res.message)
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    }
  }
}
export default deptInfo
